package com.shadowtorcher.TableRolePlayingSystem.DataStructure;

import com.shadowtorcher.TableRolePlayingSystem.util.Logger;

import java.util.ArrayList;
import java.util.HashMap;

public class DataSegment {
    public HashMap<String, SegmentItem> map;

    public DataSegment() {
        this.map = new HashMap<>();
    }

    public void put(String key, String value) {
        if ("".equals(value)) {
            Logger.debug(DataSegment.class, "警告", "值不许为空");
            return;
        }
        SegmentItem segmentItem = new SegmentItem(value);
        this.map.put(key, segmentItem);
    }

    public void put(String key, DataSegment value) {
        SegmentItem segmentItem = new SegmentItem(value);
        this.map.put(key, segmentItem);
    }

    public void put(String key, ArrayList<String> arrayList) {
        SegmentItem segmentItem = new SegmentItem(arrayList);
        this.map.put(key, segmentItem);
    }

    public String getString(String key) {
        SegmentItem dataSegment = _get(key);
        if (dataSegment == null) {
            return "null";
        } else {
            return dataSegment.getString();
        }

    }

    public DataSegment getSegment(String key) {
        SegmentItem dataSegment = _get(key);
        if (dataSegment == null) {
            return new DataSegment();
        } else {
            return dataSegment.getDataSegment();
        }
    }

    public ArrayList<String> getArray(String key) {
        SegmentItem dataSegment = _get(key);
        if (dataSegment == null) {
            return new ArrayList<>();
        } else {
            return dataSegment.getArray();
        }
    }

    @Override
    public String toString() {
        return DataProcessor.buildString(this);
    }

    private SegmentItem _get(String key) {
        return this.map.get(key);
    }
}

